package net.maku.system.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.Result;
import net.maku.framework.security.user.SecurityUser;
import net.maku.framework.security.user.UserDetail;
import net.maku.member.vo.MemberUserTokenVO;
import net.maku.system.params.CodeLoginParams;
import net.maku.system.params.QueryUserParams;
import net.maku.system.params.UnionLoginParams;
import net.maku.system.params.UpdateUserParams;
import net.maku.system.service.SysUserAuthService;
import net.maku.system.vo.LoginUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("sys/user/auth")
@Tag(name = "认证管理")
@AllArgsConstructor
public class SysUserAuthController {

    private final SysUserAuthService sysUserAuthService;

    /**
     * 通过授权的手机code和微信登陆code获取用户信息进行登陆，如果用户不存在则会注册用户
     * @param codeLoginParams
     * @return
     */
    @PostMapping("loginByCode")
    @Operation(summary = "小程序端通过微信的code登陆")
    public Result loginByCode(@RequestBody @Valid CodeLoginParams codeLoginParams){
        MemberUserTokenVO token = sysUserAuthService.loginByCode(codeLoginParams);
        return Result.ok(token);
    }

    /**
     * 通过此接口登陆的，如果用户不存在则会注册用户
     * @param unionLoginParams
     * @return
     */
    @PostMapping("loginByUnionId")
    @Operation(summary = "h5端通过微信的unionId登陆")
    public Result loginByUnionId(@RequestBody @Valid UnionLoginParams unionLoginParams){
        MemberUserTokenVO token = sysUserAuthService.loginByUnionId(unionLoginParams);
        return Result.ok(token);
    }

    /**
     * 此接口是小程序端调用，通过微信的code查询用户的基本信息，如果用户不存在注册并返回用户信息
     * @param queryUserParams
     * @return
     */
    @PostMapping("getUserInfoByCode")
    @Operation(summary = "小程序端微信的code查询用户的信息")
    public Result getUserInfoByCode(@RequestBody @Valid QueryUserParams queryUserParams){
        LoginUserVo loginUserVo = sysUserAuthService.getUserInfoByCode(queryUserParams);
        return Result.ok(loginUserVo);
    }

    /**
     * 此接口是h5端调用，通过unionId查询用户的基本信息，如果用户不存在注册并返回用户信息
     * @param unionLoginParams
     * @return
     */
    @PostMapping("getUserInfoByUnionId")
    @Operation(summary = "h5端通过微信的unionId查询用户的信息")
    public Result getUserInfoByUnionId(@RequestBody @Valid UnionLoginParams unionLoginParams){
        LoginUserVo loginUserVo = sysUserAuthService.getUserInfoByUnionId(unionLoginParams);
        return Result.ok(loginUserVo);
    }

    @PostMapping("updateUserInfo")
    @Operation(summary = "更新用户信息")
    public Result updateUserInfo(@RequestBody @Valid UpdateUserParams updateUserParams){
        sysUserAuthService.updateUserInfo(updateUserParams);
        return Result.ok();
    }

    @GetMapping("getUserInfoByMobile")
    @Operation(summary = "获取微信用户的手机号")
    public Result getUserInfoByMobile(String mobile){
        if(StringUtils.isBlank(mobile)){
            return Result.error("手机号不能为空");
        }
        LoginUserVo wxUserMobile = sysUserAuthService.getUserInfoByMobile(mobile);
        return Result.ok(wxUserMobile);
    }

    @GetMapping("getLoginInfo")
    @Operation(summary = "获取登录的用户信息")
    public Result getUserInfo(){
        UserDetail user = SecurityUser.getUser();
        LoginUserVo loginUserVo = BeanUtil.copyProperties(user, LoginUserVo.class);
        loginUserVo.setMemberId(user.getId());
        return Result.ok(loginUserVo);
    }


}
